using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using ADODB;
using Microsoft.CommerceServer.Interop.Catalog;

namespace CatalogMigration
{
	/// <summary>
	/// Summary description for CatalogSelection.
	/// </summary>
	public class frmCatalogSelection : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.CheckedListBox chkCatalogSelector;
		private System.Windows.Forms.Label lblNewCatalog;
		private System.Windows.Forms.TextBox txtNewCatalogName;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCatalogSelection()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.chkCatalogSelector = new System.Windows.Forms.CheckedListBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.lblNewCatalog = new System.Windows.Forms.Label();
			this.txtNewCatalogName = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// chkCatalogSelector
			// 
			this.chkCatalogSelector.Location = new System.Drawing.Point(0, 88);
			this.chkCatalogSelector.Name = "chkCatalogSelector";
			this.chkCatalogSelector.Size = new System.Drawing.Size(280, 229);
			this.chkCatalogSelector.TabIndex = 0;
			this.chkCatalogSelector.SelectedIndexChanged += new System.EventHandler(this.chkCatalogSelector_SelectedIndexChanged);
			// 
			// btnOK
			// 
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.Location = new System.Drawing.Point(64, 328);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(152, 328);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "Cancel";
			// 
			// lblNewCatalog
			// 
			this.lblNewCatalog.Location = new System.Drawing.Point(0, 8);
			this.lblNewCatalog.Name = "lblNewCatalog";
			this.lblNewCatalog.Size = new System.Drawing.Size(280, 48);
			this.lblNewCatalog.TabIndex = 3;
			this.lblNewCatalog.Text = "Either Select a Catalog to Import data into or Create a new Catalog by Entering n" +
				"ew name in the TextBox below.";
			// 
			// txtNewCatalogName
			// 
			this.txtNewCatalogName.Location = new System.Drawing.Point(0, 64);
			this.txtNewCatalogName.Name = "txtNewCatalogName";
			this.txtNewCatalogName.Size = new System.Drawing.Size(280, 20);
			this.txtNewCatalogName.TabIndex = 4;
			this.txtNewCatalogName.Text = "";
			// 
			// frmCatalogSelection
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(282, 357);
			this.Controls.Add(this.txtNewCatalogName);
			this.Controls.Add(this.lblNewCatalog);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.chkCatalogSelector);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "frmCatalogSelection";
			this.Text = "Catalog Selection";
			this.Load += new System.EventHandler(this.frmCatalogSelection_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			try
			{
				// check to see if we selected a new catalog name or an existing one.
				if(txtNewCatalogName.Text!=string.Empty)
				{
					// we have a new name
					frmCatalogMigrationTool.ctlgName = txtNewCatalogName.Text;
					frmCatalogMigrationTool.ctlgNew = true;
					Tools.Log(@"[Info] New Catalog name selected");
				}
				else
				{
					frmCatalogMigrationTool.ctlgName = txtNewCatalogName.Text;
					Tools.Log(@"[Info] Existing Catalog name selected");
				}
			}
			catch(Exception ex)
			{
				Tools.Log(@"[Fail] failed to select a catalog name", ex);
			}
		}

		private void chkCatalogSelector_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// only allow one checkbox to be selected.
			// get the current selected item
			int currentChkItem = chkCatalogSelector.SelectedIndex;
			
			// iterate and uncheck all items
			for(int i = 0; chkCatalogSelector.Items.Count > i; i++)
			{
				chkCatalogSelector.SetItemChecked(i, false);
			}

			// only set what was selected
			chkCatalogSelector.SetItemChecked(currentChkItem, true);
		}

		private void frmCatalogSelection_Load(object sender, System.EventArgs e)
		{
			// display the  catlaogs
			object dummy = new object();
			ADODB._Recordset rs = frmCatalogMigrationTool.catalogMgr.get_Catalogs(null, dummy, dummy, "en-US", ref dummy);

			while(!rs.EOF)
			{
				chkCatalogSelector.Items.Add(rs.Fields["CatalogName"].Value.ToString());
				rs.MoveNext();
			}
		}
	}
}
